package com.gz.EuchreU.Scene;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;

import android.util.Log;

import com.gz.EuchreU.Contants.Scenes;
import com.gz.EuchreU.Contants.UI;
import com.gz.EuchreU.Helpers.Textures;
import com.gz.EuchreU.Sprite.ButtonSprite;
import com.gz.Game.Extension.AsyncTaskLoader;
import com.gz.Game.Extension.GameScene;
import com.gz.Game.Extension.IAsyncCallback;

public class MenuScene extends GameScene{

	public static final int LAYER_TABLE = 0;
	public static final int LAYER_CARDS = LAYER_TABLE + 1;
	public static final int LAYER_HUD = LAYER_CARDS + 1;
	
	private Engine mEngine;
	
	public MenuScene(int pLayerCount, Engine baseEngine) {
		super(pLayerCount, baseEngine);
		mEngine = baseEngine;
		log("init");
	}

	@Override
	protected void onLoadResources() {
		log("Load resources");
		IAsyncCallback callback = new IAsyncCallback() {
			
			public void workToDo() {
				UI.LoadTextures(mEngine);
			}
			
			public void onComplete() {
				MenuScene.this.onLoadScene();
			}
		};
		
		new AsyncTaskLoader().execute(callback);
	}

	@Override
	protected void onLoadScene() {
		log("Load Scene");
		setOnAreaTouchTraversalFrontToBack();
		//this.getChild(LAYER_CARDS).attachChild(new Sprite(50,50,UI.getTextures().getTexture(Textures.TITLE_DECK_TEXTURE)));
		
		ButtonSprite btnNewGame = new ButtonSprite(50,50,UI.getCardWidth(),Textures.NEW_GAME_TEXTURE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					log("Opening New Game");
					Scenes.setScene(Scenes.SCENE_GAME, new EuchreGameScene(3, mEngine));
					Scenes.setCurrentScene(Scenes.SCENE_GAME);
					mEngine.setScene(Scenes.getEuchreGameScene());
				}
				return true;
			}
		};
		
		ButtonSprite btnContinue = new ButtonSprite(100,100,UI.getCardWidth(),Textures.CONTINUE_GAME_TEXTURE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					log("Opening New Game");
					Scenes.setCurrentScene(Scenes.SCENE_GAME);
					mEngine.setScene(Scenes.getEuchreGameScene());
				}
				return true;
			}
		};
		
		ButtonSprite btnSettings = new ButtonSprite(0,UI.getHeight() - UI.getCardWidth(),UI.getCardWidth(),Textures.SETTINGS_TEXTURE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					log("Opening settings");
					Scenes.setCurrentScene(Scenes.SCENE_OPTIONS);
					mEngine.setScene(Scenes.getOptionsScene());
				}
				return true;
			}
		};
		ButtonSprite btnExit = new ButtonSprite(UI.getWidth() - UI.getCardWidth(),0,UI.getCardWidth(),Textures.EXIT_TEXTURE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					log("Closing application");
					System.exit(0);
				}
				return true;
			}
		};
		
		this.getChild(LAYER_CARDS).attachChild(btnNewGame);
		this.getChild(LAYER_CARDS).attachChild(btnContinue);
		this.getChild(LAYER_CARDS).attachChild(btnSettings);
		this.getChild(LAYER_CARDS).attachChild(btnExit);
		
		registerTouchArea(btnNewGame);
		registerTouchArea(btnContinue);
		registerTouchArea(btnSettings);
		registerTouchArea(btnExit);
		Scenes.setCurrentScene(Scenes.SCENE_LOADING);
		this.mEngine.setScene(this);
	}

	@Override
	protected void unloadScene() {
		// TODO Auto-generated method stub
		log("Unload Scene");
	}

	@Override
	protected void onLoadComplete() {
		// TODO Auto-generated method stub
		log("Load Complete");
	}

	private void log(String msg){
		Log.d("Menu Scene", msg);
	}
	
}
